package com.wang.luntan.users.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.pojo.ApiResponseEnum;
import com.wang.luntan.common.pojo.JSONEnum;
import com.wang.luntan.common.util.ConstatFinalUtil;
import com.wang.luntan.common.util.PageInfoUtil;
import com.wang.luntan.users.mapper.IAtOpinionDescDao;
import com.wang.luntan.users.mapper.IAtOpinionTypeDao;
import com.wang.luntan.users.pojo.AtOpinionDesc;
import com.wang.luntan.users.pojo.AtOpinionDescEnum;
import com.wang.luntan.users.pojo.AtOpinionType;
import com.wang.luntan.users.service.IOpinionDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wjq
 * @description 针对表【at_opinion_type(意见类型表)】的数据库操作Service实现
 * @createDate 2024-02-27 22:44:21
 */
@Service
public class OpinionDbServiceImpl implements IOpinionDbService {
    @Autowired
    private IAtOpinionTypeDao opinionTypeDao;
    @Autowired
    private IAtOpinionDescDao opinionDescDao;

    @Override
    public ApiResponse<Object> saveOneOpinionTypeService(AtOpinionType users) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        Map<String, AtOpinionType> data = new HashMap<String, AtOpinionType>();
        int res = this.opinionTypeDao.save(users);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), users.getId());
            data.put(ApiResponseEnum.NAME_ONE.getName(), users);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());

        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneOpinionTypeService(AtOpinionType users) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.opinionTypeDao.update(users);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), users.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), users);

            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());

        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneOpinionTypeService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.opinionTypeDao.delete(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AtOpinionType> findOneOpinionTypeService(Map<String, Object> condMap) {
        ApiResponse<AtOpinionType> apiResponse = new ApiResponse<AtOpinionType>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, AtOpinionType> data = new HashMap<String, AtOpinionType>();
        AtOpinionType users = this.opinionTypeDao.findOne(condMap);

        data.put(ApiResponseEnum.NAME_ONE.getName(), users);
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(users);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AtOpinionType> findCondListOpinionTypeService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AtOpinionType> apiResponse = new ApiResponse<AtOpinionType>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AtOpinionType> usersList = Collections.EMPTY_LIST;

        Map<String, List<AtOpinionType>> dataList = new HashMap<String, List<AtOpinionType>>();
        /* 对关键字进行模糊匹配 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        /* 设置分页相关信息 */
        if (pageInfoUtil != null) {
            /* 设置分页信息 */
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            usersList = this.opinionTypeDao.findList(condMap);
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 向接口中放分页信息 */
            apiResponse.setPageInfoUtil(pageInfoUtil);
        } else {
            usersList = this.opinionTypeDao.findList(condMap);
        }
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), usersList);
        apiResponse.setDataListJava(usersList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> saveOneOpinionDescService(AtOpinionDesc obj) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        Map<String, AtOpinionDesc> data = new HashMap<String, AtOpinionDesc>();
        if (Objects.isNull(obj.getParentId())) {
            obj.setParentId(0);
        } else {
            /* 查询父对象 */
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("id", obj.getParentId());
            ApiResponse<AtOpinionDesc> apiResponseDbRes = this.findOneOpinionDescService(paramMap);
            AtOpinionDesc parentOpinionDesc = apiResponseDbRes.getDataOneJava();
            if (Objects.nonNull(parentOpinionDesc)) {
                obj.setTypeId(parentOpinionDesc.getTypeId());
                obj.setOpType(parentOpinionDesc.getOpType());
                obj.setName(parentOpinionDesc.getName());
                if (AtOpinionDescEnum.RESOLVESTATUS_RESO.getStatus() == parentOpinionDesc.getResolveStatus()) {
                    /* 已经解决,就没有必须再继续 */
                    apiResponse.setCode(ApiResponseEnum.INFO_SIGN_STATUS_RESOLED.getStatus());
                    apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
                    return apiResponse;
                }
            }
        }
        int res = this.opinionDescDao.save(obj);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), obj.getId());
            data.put(ApiResponseEnum.NAME_ONE.getName(), obj);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());

        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneOpinionDescService(AtOpinionDesc obj) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        AtOpinionDesc parentOpinionDesc = null;
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id", obj.getId());
        /* 查询数据库的信息 */
        ApiResponse<AtOpinionDesc> datadbResResponse = this.findOneOpinionDescService(paramMap);
        AtOpinionDesc datadbRes = datadbResResponse.getDataOneJava();
        if (Objects.nonNull(datadbRes)) {
            if (AtOpinionDescEnum.STATUS_ENABLE.getStatus() == datadbRes.getStatus()){
                /* 已经发布,无法修改 */
                apiResponse.setCode(ApiResponseEnum.INFO_SIGN_STATUS_PUBLISHED.getStatus());
                apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
                return apiResponse;
            }
        }
        /* 根据父Id查询 */
        if(obj.getParentId() != 0) {
            /* 查询父对象 */
            paramMap.clear();
            paramMap.put("id", obj.getParentId());
            ApiResponse<AtOpinionDesc> apiResponseDbRes = this.findOneOpinionDescService(paramMap);
            parentOpinionDesc = apiResponseDbRes.getDataOneJava();
            if (Objects.nonNull(parentOpinionDesc)) {
                obj.setTypeId(parentOpinionDesc.getTypeId());
                obj.setOpType(parentOpinionDesc.getOpType());
            }

            /* 如果已解决 */
            if (AtOpinionDescEnum.STATUS_ENABLE.getStatus() == obj.getStatus()
                    &&
                    AtOpinionDescEnum.RESOLVESTATUS_RESO.getStatus() == obj.getResolveStatus()) {
                /* 已发布 */
                obj.setResolveTime(new Date());
                long responseTime = obj.getResolveTime().getTime() - parentOpinionDesc.getCreateTime().getTime();
                responseTime = responseTime / 1000;
                /* 单位是秒 */
                obj.setReponseTime(responseTime);
                parentOpinionDesc.setResolveStatus(AtOpinionDescEnum.RESOLVESTATUS_RESO.getStatus());
                parentOpinionDesc.setReponseTime(responseTime);
                this.opinionDescDao.update(parentOpinionDesc);
            }
        }
        int res = this.opinionDescDao.update(obj);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), obj.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), obj);

            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());

        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneOpinionDescService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.opinionDescDao.delete(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AtOpinionDesc> findOneOpinionDescService(Map<String, Object> condMap) {
        ApiResponse<AtOpinionDesc> apiResponse = new ApiResponse<AtOpinionDesc>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        Map<String, AtOpinionDesc> data = new HashMap<String, AtOpinionDesc>();
        AtOpinionDesc users = this.opinionDescDao.findOne(condMap);

        data.put(ApiResponseEnum.NAME_ONE.getName(), users);
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(users);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AtOpinionDesc> findCondListOpinionDescService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AtOpinionDesc> apiResponse = new ApiResponse<AtOpinionDesc>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AtOpinionDesc> usersList = Collections.EMPTY_LIST;

        Map<String, List<AtOpinionDesc>> dataList = new HashMap<String, List<AtOpinionDesc>>();
        /* 对关键字进行模糊匹配 */
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
            condMap.put("keyword", "%" + condMap.get("keyword") + "%");
        }

        /* 设置分页相关信息 */
        if (pageInfoUtil != null) {
            /* 设置分页信息 */
            Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
            usersList = this.opinionDescDao.findList(condMap);
            pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
            /* 向接口中放分页信息 */
            apiResponse.setPageInfoUtil(pageInfoUtil);
        } else {
            usersList = this.opinionDescDao.findList(condMap);
        }
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), usersList);
        apiResponse.setDataListJava(usersList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}